package com.hzqy.service.epg;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.hzqy.persistence.dao.epg.AcSilverDao;
import com.hzqy.web.vo.epg.AcSilverVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.epg.AcSignDao;
import com.hzqy.web.vo.epg.AcSignVo;

@Service
public class AcSignServiceBean {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private AcSignDao acSignDao;

	@Resource
	private AcSilverDao silverDao;

	/**
	 * 查询今天签到次数*/
	public int acSignToday(String userId,String activityId) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new Date();
		String today = sdf.format(d);
		int result = acSignDao.acSignToday(userId,today,activityId);
		return result;
	}

	/**
	 * 新增用户每日签到
	 */
	public int acSignInsert(String userId,String activityId) {
		AcSignVo acsv = new AcSignVo();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = new Date();
		String today = sdf.format(d);
		acsv.setF_asu_serverid(userId);
		acsv.setF_asu_activityid(activityId);
		acsv.setF_asu_cdate(sdf.format(d).substring(0,7));
		int flag = acSignDao.acSignToday(userId,today,activityId);
		List<AcSignVo> list = acSignDao.acSignList(acsv);//总的签到信息
		LOG.debug("用户："+userId+" 总签到次数："+list.size());
		int result = 0;
		if(flag == 1 ) {
			//已签到过
			result = 1;
		}else {
			acsv.setF_asu_award(" ");
			acsv.setF_asu_obligate1("");
			acsv.setF_asu_obligate2("");
			acsv.setF_asu_cdate(sdf1.format(d));
			try {
				if(list.size()>=2&&list.size()<3){
					acsv.setF_asu_award("银子1000两和一次抽奖");
					getCardnum(userId); //绑定兑换码
				}else if(list.size()>=6&&list.size()<7){
					acsv.setF_asu_award("银子3000两和一次抽奖");
					getCardnum(userId);//绑定兑换码
				}else if(list.size()>=14&&list.size()<15){
					acsv.setF_asu_award("银子10000两和一次抽奖");
					getCardnum(userId);//绑定兑换码
				}else if(list.size()>=28&&list.size()<29){
					acsv.setF_asu_award("银子30000两和一次抽奖");
					getCardnum(userId);//绑定兑换码
				}else{
					acsv.setF_asu_award("一次签到机会");
				}
				result = acSignDao.acSignInsert(acsv);
			}catch (Exception e){
				LOG.debug("签到异常请重新签到");
				return 0;
			}
		}
		return result;
	}

	public Map<String,String> acSignList(String userId,String activity){
		Map<String,String> map = new HashMap<String,String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		AcSignVo acsv = new AcSignVo();
		acsv.setF_asu_activityid(activity);
		acsv.setF_asu_serverid(userId);
		acsv.setF_asu_cdate(sdf.format(new Date()));
		List<AcSignVo> list = acSignDao.acSignList(acsv);
		if(list.size() == AcBfCommon.monthDayNumber()) {
			LOG.debug("用户："+userId+"签到全勤---list.size()|日期："+list.size() +"|"+ AcBfCommon.monthDayNumber());
			map.put("result", "1");
			map.put("data",String.valueOf(list.size()));
		}else if(list.size() == 0){
			LOG.debug("用户："+userId+"--没有签到记录");
			map.put("result", "0");
			map.put("data",String.valueOf(list.size()));
		}else{
			List<String> listMonth = new ArrayList<String>();
			for(int i=1;i<=AcBfCommon.monthDayNumber();i++) {
				if(i<10) {
					listMonth.add(sdf.format(new Date())+"-0"+i);
				}else {
					listMonth.add(sdf.format(new Date())+"-"+i);
				}
			}
			List<String> listDate = new ArrayList<>();
			for(int i=0;i<list.size();i++) {
				System.out.println(i);
				listDate.add(list.get(i).getF_asu_cdate());
			}
			for(int i=0;i<listMonth.size();i++) {
				for(int j=0;j<listDate.size();j++) {
					if(listMonth.get(i).equals(listDate.get(j))) {
						//当月当天匹配到数据库已签到记录日期
						map.put(""+(i+1), "true");
						break;
					}
					if(j==listDate.size()-1) {
						map.put(""+(i+1), "false");
					}
				}
			}
		}
		return map;
	} 

	public static void main(String[] args) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf2 = new SimpleDateFormat("MM");
		Date d = new Date();
		List<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		System.out.println(list.toString());
		System.out.println(list.size());
		int[] ss = {1,2,3,4};
		System.out.println(ss.length);
	}

	public static int getDays(int year, int month) {
		int days = 0;
		if (month != 2) {
			switch (month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				days = 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				days = 30;
 
			}
		} else {
			// 闰年
			if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
				days = 29;
			else
				days = 28;
		}
		System.out.println("当月有" + days + "天！");
		return days;
	}

	public Map<String, Object> lotteryCount(String userId, String activityId) {
		Map<String,Object> map = new HashMap<String,Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new Date();
		String today = sdf.format(d);
		int result = acSignDao.acSignToday(userId,today,activityId);
		map.put("today", result);
		if (result > 0) {
			AcSignVo acSignVo = acSignDao.acSignInfo(userId, today, activityId);
			String count = acSignVo.getF_asu_award();
			if("".equals(count)) {
				map.put("count", 1);
			}else if("".equals(count)) {
				map.put("count", 2);
			}else if("".equals(count)) {
				map.put("count", 3);
			}else {
				map.put("count", 0);
			}
		}
		
		return map;
	}

   /** 查询兑换码和用户绑定
	* */
	public void  getCardnum(String userId){
		Map<String,Object> map=new HashMap<String,Object>();
		Map<String,Object> newMap=new HashMap<>();
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = new Date();
		String today = sdf1.format(d);
		AcSilverVo acSilverVo = silverDao.acSilverInfo(userId);
		map.put("f_as_cardkey",userId);
		map.put("f_as_cardnumber",acSilverVo.getF_as_cardnumber());
		map.put("f_as_udate",today);
		silverDao.acSilverUpdate(map);
	}
}
